﻿using UnityEngine;
using System.Collections;
using System.Linq;

public class JointB : MonoBehaviour
{

	Vector3[] latePos = new Vector3[30];
	bool key = true;
	public static bool canSee = false;
	
	public MeshRenderer[] face01;
	public MeshRenderer[] face02;
	public MeshRenderer[] face03;
	public MeshRenderer[] face04;
	public MeshRenderer[] face05;
	public MeshRenderer[] face06;
	GameObject cz1;

	public Material[] sysColors;
	
	// Use this for initialization
	void Start ()
	{
		latePos [0] = this.transform.position;
//		SetColors ();

		
	}
	
	// Update is called once per frame
	int flameCount = 0;

	void Update ()
	{
		if (this.tag == "drop") {
			if (key) {
				flameCount++;
				if (flameCount >= 30) {
					flameCount = 0;				 
					if (Vector3.Distance (this.transform.position, latePos [5]) <= 0.0007f && Vector3.Distance (this.transform.position, latePos [10]) <= 0.0007f && Vector3.Distance (this.transform.position, latePos [15]) <= 0.0007f && Vector3.Distance (this.transform.position, latePos [20]) <= 0.0007f) {
						Debug.Log ("Stop");
						Debug.Log ("pos:" + this.transform.position);
						key = false;
						GameObject.Find ("JointA").SendMessage ("con_add");
						GameObject.Find ("JointA").SendMessage ("AddBox");
						this.GetComponent<Rigidbody> ().isKinematic = true;   //避免进入平衡后isKinematic不会是ture的情况导致后面不出现下一个房子叠加放后面
						//GameObject cz = (GameObject)Instantiate (Resources.Load ("11_RFX_Bomb1"), this.transform.position - new Vector3 (0, 5, 0), this.transform.rotation);
					} 
				} else {
					latePos [flameCount] = this.transform.position;
				}
			}
		} else if (this.tag == "HitGround") {
			Destroy (this.gameObject);
			GameObject.Find ("JointA").SendMessage ("erro_add");
			GameObject.Find ("JointA").SendMessage ("Restart");
		} 
	}

	void stop_tb ()
	{
		
	}

	void SetColors ()
	{
		int c = Random.Range (0, 24);
		MeshRenderer[] tmp0 = face01.OrderBy (x => System.Guid.NewGuid ()).ToArray ();
		for (int i = 0; i < 8; i++) {
			tmp0 [i].material = sysColors [c];
		}
		MeshRenderer[] tmp1 = face02.OrderBy (x => System.Guid.NewGuid ()).ToArray ();
		for (int i = 0; i < 8; i++) {
			tmp1 [i].material = sysColors [c];
		}
		MeshRenderer[] tmp2 = face03.OrderBy (x => System.Guid.NewGuid ()).ToArray ();
		for (int i = 0; i < 8; i++) {
			tmp2 [i].material = sysColors [c];
		}
		MeshRenderer[] tmp3 = face04.OrderBy (x => System.Guid.NewGuid ()).ToArray ();
		for (int i = 0; i < 8; i++) {
			tmp3 [i].material = sysColors [c];
		}
		MeshRenderer[] tmp4 = face05.OrderBy (x => System.Guid.NewGuid ()).ToArray ();
		for (int i = 0; i < 8; i++) {
			tmp4 [i].material = sysColors [c];
		}
		MeshRenderer[] tmp5 = face06.OrderBy (x => System.Guid.NewGuid ()).ToArray ();
		for (int i = 0; i < 8; i++) {
			tmp5 [i].material = sysColors [c];
		}
	}

	void OnCollisionEnter (Collision other)
	{

		if (other.gameObject.name != "Ground" && this.gameObject.GetComponent<Rigidbody> ().isKinematic == false) {
			cz1 = (GameObject)Instantiate (Resources.Load ("RFX_TowerBlox"), this.transform.GetChild (0).position, this.transform.rotation);
			if (other.gameObject.name == "Ground") {
				this.gameObject.tag = "HitGround";
			}
		} else {
			if (other.gameObject.name == "Ground") {
				this.gameObject.tag = "HitGround";
			}
		}
	}
}
